local Musha_Fatiguesleep =
	Class(
	function(self, inst)
		self.inst = inst

		if TheWorld.ismastersim then
			self.classified = inst.player_classified
		elseif self.classified == nil and inst.player_classified ~= nil then
			self:AttachClassified(inst.player_classified)
		end
	end
)

function Musha_Fatiguesleep:OnRemoveFromEntity()
	if self.classified ~= nil then
		if TheWorld.ismastersim then
			self.classified = nil
		else
			self.inst:RemoveEventCallback("onremove", self.ondetachclassified, self.classified)
			self:DetachClassified()
		end
	end
end

Musha_Fatiguesleep.OnRemoveEntity = Musha_Fatiguesleep.OnRemoveFromEntity

function Musha_Fatiguesleep:AttachClassified(classified)
	self.classified = classified
	self.ondetachclassified = function()
		self:DetachClassified()
	end
	self.inst:ListenForEvent("onremove", self.ondetachclassified, classified)
end

function Musha_Fatiguesleep:DetachClassified()
	self.classified = nil
	self.ondetachclassified = nil
end

--------------------------------------------------------------------------

function Musha_Fatiguesleep:SetCurrent(current)
	if self.classified ~= nil then
		self.classified:SetValue("currentmusha_fatiguesleep", current)
	end
end

function Musha_Fatiguesleep:SetMax(max)
	if self.classified ~= nil then
		self.classified:SetValue("maxmusha_fatiguesleep", max)
	end
end

function Musha_Fatiguesleep:Max()
	if self.inst.components.musha_fatiguesleep ~= nil then
		return self.inst.components.musha_fatiguesleep.max
	elseif self.classified ~= nil then
		return self.classified.maxmusha_fatiguesleep:value()
	else
		return 100
	end
end

function Musha_Fatiguesleep:GetPercent()
	if self.inst.components.musha_fatiguesleep ~= nil then
		return self.inst.components.musha_fatiguesleep:GetPercent()
	elseif self.classified ~= nil then
		return self.classified.currentmusha_fatiguesleep:value() / self.classified.maxmusha_fatiguesleep:value()
	else
		return 0
	end
end

function Musha_Fatiguesleep:GetCurrent()
	if self.inst.components.musha_fatiguesleep ~= nil then
		return self.inst.components.musha_fatiguesleep.current
	elseif self.classified ~= nil then
		return self.classified.currentmusha_fatiguesleep:value()
	else
		return 0
	end
end

return Musha_Fatiguesleep
